<?php
class siso_receipt_sales_sold extends siso_receipt_sales_abstract implements siso_receipt_sales_interface{

    /**
     *
     * 根据参数组织订单销售的销售单数据
     * @param array $params
     */
    public function get_sales_data($params){
        $order_original_data = array();
        $sales_data = array();
        $delivery_id = $params['delivery_id'];

        $deliveryObj = app::get('ome')->model('delivery');
        $orderIds = $deliveryObj->getOrderIdsByDeliveryIds(array($delivery_id));

        $ome_original_dataLib = kernel::single('ome_sales_original_data');
        $ome_sales_dataLib = kernel::single('ome_sales_data');
        foreach ($orderIds as $key => $orderId){
            $order_original_data = $ome_original_dataLib->init($orderId);
            if($order_original_data){
                $sales_data[$orderId] = $ome_sales_dataLib->generate($order_original_data,$delivery_id);
                if(!$sales_data[$orderId]){
                    return false;
                }
            }else{
                return false;
            }
            unset($order_original_data);
        }

        //平摊预估物流运费，主要处理订单合并发货以及多包裹单的运费问题
        $ome_sales_logistics_feeLib = kernel::single('ome_sales_logistics_fee');
        $ome_sales_logistics_feeLib->calculate($orderIds,$sales_data);

        $this->format_sales_items($sales_data,$delivery_id);

        return $sales_data;

    }

    /**
    * 余单撤销数据组织
    *
    */
    private function format_sales_items(&$sales_data,$delivery_id){
    
        $orderObj = app::get('ome')->model('orders');
        $delivery_items_detailObj = app::get('ome')->model('delivery_items_detail');

        foreach ($sales_data as $order_id=>$sales){
            $order_items    = $detail_items = $temp_arr = array();
            $orders = $orderObj->dump($order_id,'process_status');
            if (in_array($orders['process_status'],array('remain_cancel'))){

                $sql    = "SELECT a.item_id, a.obj_id, a.nums, a.sendnum, a.item_type, b.quantity 
                            FROM sdb_ome_order_items AS a LEFT JOIN sdb_ome_order_objects AS b ON a.obj_id=b.obj_id 
                            WHERE a.order_id='".$order_id."'";
                $temp_arr   = kernel::database()->select($sql);
                foreach ($temp_arr as $key => $val){
                    $item_id    = $val['item_id'];
                    
                    $val['dly_nums']        = intval($val['sendnum'] / $val['nums'] * $val['quantity']);//已发货数量
                    $order_items[$item_id]  = $val;
                }
                
               
                $temp_arr   = $delivery_items_detailObj->getList('item_detail_id, order_item_id, order_obj_id, number', array('order_id'=>$order_id));
                foreach ($temp_arr as $key => $val)
                {
                    $item_detail_id     = $val['item_detail_id'];
                    $order_item_id      = $val['order_item_id'];
                    
                    $val['dly_nums']    = intval($order_items[$order_item_id]['dly_nums']);//总发货数量
                    
                    $detail_items[$item_detail_id]  = $val;
                }
                foreach ($sales['sales_items'] as $key => &$item){
                    $item_detail_id     = $item['item_detail_id'];
                    $nums               = intval($item['nums']);//购买数量
                    $send_num           = $detail_items[$item_detail_id]['dly_nums'];//发货数量
                    
                    $item['pmt_price']      = round(($item['pmt_price'] / $nums * $send_num), 2);
                    $item['sale_price']     = round(($item['sale_price'] / $nums * $send_num), 2);
                    $item['sales_amount']   = round(($item['sales_amount'] / $nums * $send_num), 2);
                    $item['nums']           = $send_num;
                }
            }
        }
    }
}